// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using System;
using System.Diagnostics;

namespace SoapTrace.Core.Communication
{
	/// <summary>
	/// A field in the soap header.
	/// </summary>
	public class SoapHeaderField
	{
		/// <summary>
		/// The name of the field.
		/// </summary>
		private string m_Name = String.Empty;

		/// <summary>
		/// The value of the field.
		/// </summary>
		private string m_Value = String.Empty;

		/// <summary>
		/// Characters to be removed from any field value.
		/// </summary>
		private char[ ] m_TrimCharacters = new char[ ] { ' ', '\t', '\r', '\n' };

		/// <summary>
		/// Creates a new instance of the <see cref="SoapHeaderField"/> class.
		/// </summary>
		/// <param name="fieldText">The Http header line containing one
		/// field-value pair.</param>
		public SoapHeaderField( string fieldText )
		{
			ParseLine( fieldText );
		}

		/// <summary>
		/// Creates a new instance of the <see cref="SoapHeaderField"/> class.
		/// </summary>
		public SoapHeaderField( )
		{
		}

		/// <summary>
		/// Gets or sets the name of the header field.
		/// </summary>
		public string Name
		{
			[ DebuggerStepThrough ]
			get
			{
				return m_Name;
			}

			[ DebuggerStepThrough ]
			set
			{
				m_Name = value;
			}
		}

		/// <summary>
		/// Gets or sets the field value.
		/// </summary>
		public string Value
		{
			[ DebuggerStepThrough ]
			get
			{
				return m_Value;
			}

			[ DebuggerStepThrough ]
			set
			{
				m_Value = value;
			}
		}

		/// <summary>
		/// Parses a field name line of the form "Name: Value".
		/// </summary>
		/// <param name="text"></param>
		private void ParseLine( string text )
		{
			int ColonIndex = text.IndexOf( ":" );

			if( ColonIndex != -1 )
			{
				m_Name = text.Substring( 0, ColonIndex );

				m_Value = text.Substring( ColonIndex + 1 );
				m_Value = m_Value.Trim( m_TrimCharacters );
			}
			else
			{
				m_Name = text;
			}
		}
	}
}
